package deviceMng

import "errors"

type DeviceMng struct {
	Device map[string]DeviceOption
}

var gDeviceMng *DeviceMng

func init() {
	gDeviceMng = NewDeviceMng()
}

func GlobalDeviceMng() *DeviceMng {
	return gDeviceMng
}

func NewDeviceMng() *DeviceMng {
	dm := DeviceMng{}
	dm.Device = make(map[string]DeviceOption, 10)
	return &dm
}

func (dm *DeviceMng) AddNew(option DeviceOption) error {
	if _, ok := dm.Device[option.Addr]; ok {
		// 找到了
		return nil
	}
	// 没找到
	dm.Device[option.Addr] = option
	return nil
}

func (dm *DeviceMng) Find(option DeviceOption) *DeviceOption {
	if val, ok := dm.Device[option.Addr]; ok {
		return &val
	}
	return nil
}

func (dm *DeviceMng) Remove(option DeviceOption) error {
	if _, ok := dm.Device[option.Addr]; ok {
		delete(dm.Device, option.Addr)
		return nil
	}
	return errors.New("not exist")
}
